//
//  MyTools.m
//  myappinSchool1.0
//
//  Created by XuLee on 15/7/22.
//  Copyright (c) 2015年 XuLee. All rights reserved.
//

#import "MyTools.h"

@implementation MyTools
#pragma mark 判断手机是否联网
+ (BOOL)connectedToNetwork{
    
    //创建零地址，0.0.0.0的地址表示查询本机的网络连接状态
    
    struct sockaddr_storage zeroAddress;
    
    bzero(&zeroAddress, sizeof(zeroAddress));
    zeroAddress.ss_len = sizeof(zeroAddress);
    zeroAddress.ss_family = AF_INET;
    
    // Recover reachability flags
    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
    SCNetworkReachabilityFlags flags;
    
    //获得连接的标志
    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
    CFRelease(defaultRouteReachability);
    
    //如果不能获取连接标志，则不能连接网络，直接返回
    if (!didRetrieveFlags)
    {
        return NO;
    }
    //根据获得的连接标志进行判断
    
    BOOL isReachable = flags & kSCNetworkFlagsReachable;
    BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
    return (isReachable&&!needsConnection) ? YES : NO;
}


+ (AFHTTPRequestOperationManager *)initAFHttpManager {
    static AFHTTPRequestOperationManager *manager;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        manager = [[AFHTTPRequestOperationManager alloc] init];
        manager.responseSerializer = [AFJSONResponseSerializer serializer];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];

        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain", nil];
        manager.operationQueue.maxConcurrentOperationCount = 1;
    });
    
    return manager;
}


+(NSDictionary *)myAF:(NSString *)url and:(nullable id)message{
    static AFHTTPRequestOperationManager *manager;
    manager = [MyTools initAFHttpManager];
    static NSMutableDictionary *dic;
    dic = [[NSMutableDictionary alloc]init];
    
    [manager POST:url parameters:message success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        [dic setDictionary:responseObject];
        
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        if ([MyTools connectedToNetwork] == 0) {
            [dic setObject:@"没有联网" forKey:@"message"];
        }else{
            [dic setObject:@"服务器错误，请刷新重试" forKey:@"message"];
        }
        [dic setObject:@"1" forKey:@"isSuccess"];
    }];
    return dic;
}



@end
